package com.jy.project.service.imp;

import com.jy.project.domain.MaterialAttrField;
import com.jy.project.enums.RequiredTypeEnum;
import com.jy.project.vo.MaterialAttrFieldVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.jy.project.mapper.MaterialAttrFieldMapper;
import com.jy.project.service.MaterialAttrFieldService;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MaterialAttrFieldServiceImpl implements MaterialAttrFieldService {

    @Resource
    private MaterialAttrFieldMapper materialAttrFieldMapper;

    @Override
    public void insertBatch(List<MaterialAttrField> materialAttrFieldList) {
        Date date = new Date();
        for (MaterialAttrField materialAttrField : materialAttrFieldList) {
            materialAttrField.setUpdateAt(date);
            materialAttrField.setIsDelete(0);
            materialAttrField.setCreateAt(date);
            materialAttrFieldMapper.insertSelective(materialAttrField);
        }
    }

    @Override
    public List<MaterialAttrFieldVO> listMaterialAttrField(String dictNo) {
        MaterialAttrField example = new MaterialAttrField();
        example.setIsDelete(0);
        example.setDictNo(dictNo);
        List<MaterialAttrField> materialAttrFieldList = materialAttrFieldMapper.select(example);
        return materialAttrFieldList.stream().map(materialAttrField -> {
            MaterialAttrFieldVO materialAttrFieldVO = new MaterialAttrFieldVO();
            return materialAttrFieldVO.setFieldId(materialAttrField.getId())
                    .setFieldName(materialAttrField.getFieldName())
                    .setRequired(materialAttrField.getRequired())
                    .setRequiredValue(RequiredTypeEnum.getValue(materialAttrField.getRequired()))
                    .setFieldType(materialAttrField.getFieldType());
        }).collect(Collectors.toList());
    }

    @Override
    public void deleteById(Long id) {
        materialAttrFieldMapper.deleteByPrimaryKey(id);
    }
}
